<template>
  <div class="topHeader">
    <div class="topLeftHeader">
      <div>
        <el-breadcrumb separator="/">
          <el-breadcrumb-item v-for="item in breadcrumb">{{ item.name }}</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
    </div>
    <div class="topRightHeader">
      <el-dropdown>
        <div class="userInfo">
          <div class="userImg">{{ user.username.slice(0, 1) }}</div>
          <div class="userName">{{ user.username }}</div>
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item @click="openUserMobileDialog">{{user.mobile ? '修改手机号' : '设置手机号'}}</el-dropdown-item>
            <el-dropdown-item @click="useExit">退出登录</el-dropdown-item>
            <el-dropdown-item />
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
    <el-dialog
        draggable title="添加新员工" :model-value="userMobileDialogVisible" @closed="userMobileDialogClose" width="400">
      <el-form @submit="updateUserMobile">
        <el-form-item label="手机号">
          <el-input
              v-model="userMobileInputStr"
              placeholder="请输入手机号"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" native-type="submit">确认</el-button>
          <el-button @click="userMobileDialogClose">关闭</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup>
import {getBreadcrumbs} from "../composition/useRouterLink.js";
import {useExit} from "../composition/useUser.js";
import {useUpdateUserMobile, useUserInfoRef} from "@/composition/billSys/useAccount.js";
import {elTip, validatePhone} from "@/utils/index.js";

const breadcrumb = getBreadcrumbs();
const user = useUserInfoRef();


//-------------------------------------------------2-------------------------------------------------------
// 展示修改或者设置手机号的dialog
const userMobileInputStr = ref('');
const userMobileDialogVisible = ref(false);

// 更新用户手机号
const updateUserMobile = (e) => {
  e.preventDefault();
  if(!validatePhone(userMobileInputStr.value)) {
    return elTip('手机号格式错误');
  }
  useUpdateUserMobile(userMobileInputStr.value);
  userMobileDialogClose();
};

// 关闭dialog
const userMobileDialogClose = () => {
  userMobileDialogVisible.value = false;
  userMobileInputStr.value = user.value.mobile;
};

// 开启弹窗
const openUserMobileDialog = () => {
  userMobileDialogVisible.value = true;
};


onMounted(() => {
  userMobileInputStr.value = user.value.mobile;
});

</script>

<style scoped lang="scss">
.topHeader {
  width: 100%;
  height: $header-height;
  border-bottom: 1px solid $split-line-color;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;

  .topLeftHeader {
    padding: 0 10px;
  }

  .topRightHeader {
    display: flex;
    flex-direction: row;
    align-items: center;
    font-size: 16px;
    height: 100%;
    padding: 0 10px;

    &:hover {
      background-color: $bg-color-gray;
    }
    .userInfo {
      display: flex;
      flex-direction: row;
      align-items: center;
      .userImg {
        display: flex;
        justify-content: center;
        color: white;
        width: 32px;
        height: 32px;
        line-height: 32px;
        border-radius: 16px;
        position: relative;
        background-color: red;
        margin-right: 10px;
      }
    }

  }
}
</style>
